<?php

class M_awards extends CI_Model {

	function __construct(){
		parent::__construct();
	}

	function awards_get_count_rows($query){
		$sql="SELECT *
				FROM awards";
		$num_rows = $this->m_public_function->get_count_rows($sql);
		return $num_rows;
	}

	function awards_listview($query, $start, $end){

		$sql="SELECT *
				FROM awards
				ORDER BY id DESC";

		$num_rows = $this->m_public_function->get_count_rows($sql);
		$sql.=" LIMIT $start, $end";
		//$this->firephp->log($sql);

		$query = $this->db->query($sql);
		if($query->num_rows()>0){
			return $query->result();
		}else{
			return null;
		}

	}

	function awards_list($search_text, $start, $end){

		$sql="SELECT
				id,
				image,
				deskripsi,
				link
			FROM
				awards
			WHERE id IS NOT NULL 
			";
		if($search_text != ''){
			$sql .= " AND ( lower(deskripsi) LIKE '%".$search_text."%' 
			)";
		}
		$num_rows = $this->m_public_function->get_count_rows($sql);
		if($start != 0 && $end != 0){
			$sql.=" LIMIT $start, $end";
		}
		$query = $this->db->query($sql);

		if($num_rows > 0){
			$result = $query->result();
			$i = 0;
			foreach($result as $row){
				$data[$i] = $row;
				$i++;
			}
		}
		$final[] = array(
			"TotalRows"=>$num_rows,
			"results"=>$data
		);
		return json_encode($final);
	}

	function awards_save($params){
		extract($params);
		if($task == 'create'){
			$this->db->insert('awards',$data);
		}else{
			$this->db->where('id', $id);
			$this->db->update('awards',$data);
		}
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

	function getDetail($id){
		$sql = "
			SELECT
				*
			FROM awards
			WHERE id = ".$id."
		";
		$query = $this->db->query($sql);

		$num_rows = $query->num_rows();
		if($num_rows > 0){
			return $query->row();
		}
	}

	function awards_delete($id){
		$this->db->where('id',$id);
		$this->db->delete('awards');
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

}